from selenium.webdriver.common.alert import Alert
from selenium.webdriver.common.by import By
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from common.Utils import GobangDriver


class GobangLogin:
    url = ""
    driver = ""
    alert = ""
    wait = WebDriverWait(driver, 15)
    def __init__(self):
        self.url = "http://39.99.235.0:8888/login.html"
        self.driver = GobangDriver.driver
        self.driver.get(self.url)
    def loginPage(self):
        self.driver.find_element(By.CSS_SELECTOR,"body > div.login-container > div > h3")

    def loginSucTest(self):
        self.driver.find_element(By.CSS_SELECTOR,"#user_name").clear()
        self.driver.find_element(By.CSS_SELECTOR,"#password").clear()
        self.driver.find_element(By.CSS_SELECTOR,"#user_name").send_keys("wangwu")
        self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("1111")
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
        alert = WebDriverWait(self.driver, 10).until(EC.alert_is_present())
        alert = self.driver.switch_to.alert

        alert.accept()#可以连续确认多个弹窗?
        # alert.accept()
        GobangDriver.getScreenShoot()#无法截取弹窗?
        self.driver.find_element(By.CSS_SELECTOR,"#screen > p")

    def loginFailTest(self):

            #只输入账号
            #只输入密码
            #已有账号 正确密码
            #已有账号 错误密码
            self.driver.find_element(By.CSS_SELECTOR, "#user_name").clear()
            self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
            self.driver.find_element(By.CSS_SELECTOR, "#user_name").send_keys("张三")
            self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
            alert = WebDriverWait(self.driver, 10).until(EC.alert_is_present())
            # alert = self.driver.switch_to.alert
            # assert alert.text == "用户名密码错误"  # assert alert.get().equals("");
            # alert.accept()
            # 切换到弹窗
            alert = Alert(self.driver)

            # 获取弹窗文本并断言
            assert alert.text == "用户名密码错误", f"弹窗文本不符合预期，实际内容为：{alert.text}"
            print("测试通过：弹窗文本符合预期")

            # 关闭弹窗
            alert.accept()
            GobangDriver.getScreenShoot()

            # self.driver.find_element(By.CSS_SELECTOR, "#user_name").clear()
            # self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
            # self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("1111")
            # self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
            # alert = WebDriverWait(self.driver, 10).until(EC.alert_is_present())
            # alert = self.driver.switch_to.alert
            # assert alert.text == "用户名密码错误"  # assert alert.get().equals("");            alert.accept()
            #
            # self.driver.find_element(By.CSS_SELECTOR, "#user_name").clear()
            # self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
            # self.driver.find_element(By.CSS_SELECTOR, "#user_name").send_keys("wangwu")
            # self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("1")
            # self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
            # alert = WebDriverWait(self.driver, 10).until(EC.alert_is_present())
            # assert alert.text == "用户名密码错误"  # assert alert.get().equals("");            alert.accept()
            #
            # self.driver.find_element(By.CSS_SELECTOR, "#user_name").clear()
            # self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
            # self.driver.find_element(By.CSS_SELECTOR, "#user_name").send_keys("wangwu")
            # self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("1111")
            # self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
            # alert = WebDriverWait(self.driver, 10).until(EC.alert_is_present())
            # assert alert.text == "用户名密码错误"  # assert alert.get().equals("");            alert = self.driver.switch_to.alert
            # alert.accept()
            # # alert.accept()
            # GobangDriver.getScreenShoot()
            # self.driver.find_element(By.CSS_SELECTOR, "#screen > p")
login = GobangLogin()
# login.loginSucTest()
login.loginFailTest()